package com.lg.cms.controller.admin.article;

import com.baomidou.mybatisplus.extension.api.R;
import com.lg.cms.entity.Tags;
import com.lg.cms.service.TagsService;
import com.lg.cms.utils.SubjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description: 标签管理
 * @Author: wuyuhang
 * @create: 2020-10-05 08:51
 */
//@RestController
@Controller
@RequestMapping("/admin/tags")
@Api(tags = "标签管理")
public class WYHTagsController {
    @Autowired
    TagsService tagsService;

    @GetMapping("/list")
    public String list(Model model, HttpServletRequest request){
        model.addAttribute("user", SubjectUtil.getSuperUser());
        model.addAttribute("request", request);
        model.addAttribute("tagsList", tagsService.list());
        return "/admin/wyh-test/article/tags_list";
    }

    @ApiOperation("根据id获取标签信息")
    @GetMapping("/get/{id}")
    @ResponseBody
    public R<?> getById(@PathVariable("id") Integer id){
        Tags tags = tagsService.getById(id);
        if(tags == null){
            return R.failed("该标签不存在");
        }
        return R.ok(tags);
    }

    /**
     * 新增标签
     * @param tags
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("新增标签")
    public R<?> add(Tags tags){
        if(!tags.checkData()){
            return R.failed("请输入必填数据");
        }
        boolean result = tagsService.save(tags);
        if(!result){
            return R.failed("新增标签失败");
        }
        return R.ok(tags);
    }

    /**
     * 修改标签
     * @param tags
     * @return
     */
    @PutMapping("/update")
    @ResponseBody
    @ApiOperation("修改标签")
    public R<?> update(Tags tags){
        if(tags.getId() == null){
            return R.failed("修改id不能为空");
        }
        if(!tags.checkData()){
            return R.failed("请输入必填数据");
        }
        Tags oldTags = tagsService.getById(tags.getId());
        if(oldTags == null){
            return R.failed("此标签不存在");
        }
        oldTags.setName(tags.getName());
        oldTags.setDescription(tags.getDescription());
        boolean result = tagsService.updateById(oldTags);
        if(!result){
            return R.failed("修改标签失败");
        }
        return R.ok(tags);
    }

    /**
     * 删除.批量删除
     * @param idArray
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    @ApiOperation("删除标签")
    public R<?> delete(int[] idArray){
        Set<Integer> idSet = new HashSet<>();
        for(int i: idArray){
            idSet.add(i);
        }
        boolean result = tagsService.removeByIds(idSet);
        if(!result){
            return R.failed("删除失败");
        }
        return R.ok(null);
    }
}